package com.yswl.yeeduo.ui.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.tool.KLog;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.fragment.LuckyMoneyActivityFragment;
import com.yswl.yeeduo.ui.fragment.MyMoneyFragment;
import com.yswl.yeeduo.ui.tool.PositionUtil;

/**
 * 首页
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = MainActivity.class.getSimpleName();
	Button taBtn, taBtn2;
	Map<String, Fragment> fragmentMap = new HashMap<String, Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		removeAllActivity(this);
		
		KLog.e("JPush-RegistrationID", JPushInterface.getRegistrationID(this));
		new PositionUtil(this).startRegin();
		attachFragment();
		initNav();
		initUI();

	}

	private void attachFragment() {
		// 加入集合
		fragmentMap.put(LuckyMoneyActivityFragment.TAG, getFragmentByTagFromMap(getSupportFragmentManager(), LuckyMoneyActivityFragment.TAG, LuckyMoneyActivityFragment.class));
		fragmentMap.put(MyMoneyFragment.TAG, getFragmentByTagFromMap(getSupportFragmentManager(), MyMoneyFragment.TAG, MyMoneyFragment.class));

		// 显示首页
		FragmentManager fm = getSupportFragmentManager();
		Fragment fragment1 = fm.findFragmentByTag(LuckyMoneyActivityFragment.TAG);
		if (fragment1 != null) {
			fm.beginTransaction().show(fragment1).commit();
		} else {
			fm.beginTransaction().replace(R.id.content_frame, fragmentMap.get(LuckyMoneyActivityFragment.TAG), LuckyMoneyActivityFragment.TAG).commit();
		}
	}

	private void initNav() {
		mBack = (ImageView) findViewById(R.id.nav_left);
		mTitle = (TextView) findViewById(R.id.nav_title);
		mTitle.setText("一点");
		mBack.setVisibility(View.INVISIBLE);
	}

	private void initUI() {
		taBtn = (Button) findViewById(R.id.share_btn);
		taBtn.setOnClickListener(this);
		setButtonState(taBtn, true);
		taBtn2 = (Button) findViewById(R.id.my_money_btn);
		taBtn2.setOnClickListener(this);
		setButtonState(taBtn2, false);
	}

	private void setButtonState(Button taBtn, boolean isfocus) {
		if (isfocus) {
			taBtn.setBackgroundColor(getResources().getColor(R.color.nav_bg_color));
			taBtn.setTextColor(getResources().getColor(R.color.white));
		} else {
			taBtn.setBackgroundColor(getResources().getColor(R.color.white));
			taBtn.setTextColor(getResources().getColor(android.R.color.black));
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.share_btn:
				setButtonState(taBtn, true);
				setButtonState(taBtn2, false);
				switchContent(getCurruVisibleFragmentFromMap(), fragmentMap.get(LuckyMoneyActivityFragment.TAG));
			break;
		case R.id.my_money_btn:
				setButtonState(taBtn, false);
				setButtonState(taBtn2, true);
				switchContent(getCurruVisibleFragmentFromMap(),fragmentMap.get( MyMoneyFragment.TAG));

			break;

		default:
			break;
		}
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private Fragment getCurruVisibleFragmentFromMap() {
		FragmentManager fragmentManager = getSupportFragmentManager();
		List<Fragment> fragments = fragmentManager.getFragments();
		if (fragments == null)
			return null;
		for (Fragment currfragmet : fragments) {
			if (currfragmet != null && currfragmet.isVisible())
				return currfragmet;
		}
		return null;
	}

	private Fragment getFragmentByTagFromMap(FragmentManager frgmentManager, String tag, Class<? extends Fragment> clazz) {
		try {
			Fragment fragment = frgmentManager.findFragmentByTag(tag);
			if (fragment == null) {
				fragment = clazz.newInstance();
			} else {
				if (fragment.isAdded() && !fragment.isHidden()) {
					frgmentManager.beginTransaction().hide(fragment).commit();
				}
			}

			return fragment;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return null;
	}

	private long mExitTime;

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				exitApp();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
